/*	
 *				
 *					ISNW ---- It is a war.It is not warcraft!
 *
 *
 *					Author:					zhoukeli(haxixi_keli)
 *					Filename:				isnwMaterial.h
 *					CreateTime:				2008/12/19
 *					ModifyTime:				2008/12/19
 *
 */

#ifndef __ISNW_MATERIAL_H__
#define __ISNW_MATERIAL_H__

#include "isnwTypes.h"
#include "isnwColor.h"
#include "MaterialLayer.h"

namespace ISNW
{
namespace video
{
	const u32 MAX_TEXTURE_LAYERS = 2;
	class material
	{
	public:
		//! members

		//! material may have a name
		core::string	name;

		//! Ambient color
		video::color32 ambient;
		//! Diffuse color
		video::color32 diffuse;
		//! Emissive color
		video::color32 emissive;
		//! Specular color
		video::color32 specular;

		//! Face color
		video::color32 facecolor;

		//! power
		f32		power;

		MaterialLayer	texturelayers[MAX_TEXTURE_LAYERS];

	public:
		//! constructor
		material():power(0){}
		material(const color32& a,const color32& d,const color32& s,const color32 e)
			:ambient(a),diffuse(d),specular(s),emissive(e){}

	public:
		//! operators
		material& operator = (const material& other)
		{
			ambient = other.ambient;
			diffuse = other.diffuse;
			specular = other.specular;
			emissive = other.emissive;

			facecolor = other.facecolor;

			power = other.power;

			for(u32 i = 0; i < MAX_TEXTURE_LAYERS; ++i)
			{
				texturelayers[i] = other.texturelayers[i];
			}

			return *this;
		}

		void setTexture(u32 stage,ITexture* tex)
		{
			if(stage > MAX_TEXTURE_LAYERS) return;

			if(texturelayers[stage].texture)
			{
				texturelayers[stage].texture->drop();
			}

			texturelayers[stage].texture = tex;

			if(tex)
			{
				tex->grab();
			}
		}

		void removeAllTexture()
		{
			for(u32 i = 0; i < MAX_TEXTURE_LAYERS; ++i)
			{
				if(texturelayers[i].texture)
				{
					texturelayers[i].texture->drop();
				}
			}
		}

	};
}
}
#endif
